#include "resource_manager.h"

SDL_Texture* ResourceManager::getTexture(const std::string& path)
{
    std::map<std::string, SDL_Texture*>::iterator item = textures.find(path);
    if (item != textures.end()) {
        return item->second;
    }
    char* bmpPath = nullptr;
    SDL_asprintf(&bmpPath, "%sres/%s", SDL_GetBasePath(), path.c_str());
    SDL_Surface* surface = SDL_LoadBMP(bmpPath);
    if (surface == nullptr) {
        SDL_Log("Failed to load image: %s\n", SDL_GetError());
        return nullptr;
    }
    SDL_free(bmpPath);

    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
    if (texture == nullptr) {
        SDL_Log("Failed to create texture: %s\n", SDL_GetError());
        return nullptr;
    }
    SDL_DestroySurface(surface);
    textures[path] = texture;
    return texture;
}